from datetime import datetime
from fastapi import APIRouter, Depends, Form, Request
from pydantic_validation_decorator import ValidateFields
from sqlalchemy.ext.asyncio import AsyncSession
from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_plant.service.plot_service import PlotService
from module_plant.entity.vo.plot_vo import DeletePlotModel, PlotModel, PlotPageQueryModel
from utils.common_util import bytes2file_response
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil


plotController = APIRouter(prefix='/plant/plot', dependencies=[Depends(LoginService.get_current_user)])


@plotController.get(
    '/list', response_model=PageResponseModel, dependencies=[Depends(CheckUserInterfaceAuth('plant:plot:list'))]
)
async def get_plant_plot_list(
    request: Request,
plot_page_query: PlotPageQueryModel = Depends(PlotPageQueryModel.as_query),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    plot_page_query_result = await PlotService.get_plot_list_services(query_db, plot_page_query, is_page=True)
    logger.info('获取成功')

    return ResponseUtil.success(model_content=plot_page_query_result)


@plotController.post('', dependencies=[Depends(CheckUserInterfaceAuth('plant:plot:add'))])
@ValidateFields(validate_model='add_plot')
@Log(title='种植地块', business_type=BusinessType.INSERT)
async def add_plant_plot(
    request: Request,
    add_plot: PlotModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    add_plot.create_time = datetime.now()
    add_plot.create_by = current_user.user.user_name
    add_plot.update_time = datetime.now()
    add_plot.update_by = current_user.user.user_name
    add_plot_result = await PlotService.add_plot_services(query_db, add_plot)
    logger.info(add_plot_result.message)

    return ResponseUtil.success(msg=add_plot_result.message)


@plotController.put('', dependencies=[Depends(CheckUserInterfaceAuth('plant:plot:edit'))])
@ValidateFields(validate_model='edit_plot')
@Log(title='种植地块', business_type=BusinessType.UPDATE)
async def edit_plant_plot(
    request: Request,
    edit_plot: PlotModel,
    query_db: AsyncSession = Depends(get_db),
    current_user: CurrentUserModel = Depends(LoginService.get_current_user),
):
    edit_plot.update_by = current_user.user.user_name
    edit_plot.update_time = datetime.now()
    edit_plot_result = await PlotService.edit_plot_services(query_db, edit_plot)
    logger.info(edit_plot_result.message)

    return ResponseUtil.success(msg=edit_plot_result.message)


@plotController.delete('/{plot_ids}', dependencies=[Depends(CheckUserInterfaceAuth('plant:plot:remove'))])
@Log(title='种植地块', business_type=BusinessType.DELETE)
async def delete_plant_plot(request: Request, plot_ids: str, query_db: AsyncSession = Depends(get_db)):
    delete_plot = DeletePlotModel(plotIds=plot_ids)
    delete_plot_result = await PlotService.delete_plot_services(query_db, delete_plot)
    logger.info(delete_plot_result.message)

    return ResponseUtil.success(msg=delete_plot_result.message)


@plotController.get(
    '/{plot_id}', response_model=PlotModel, dependencies=[Depends(CheckUserInterfaceAuth('plant:plot:query'))]
)
async def query_detail_plant_plot(request: Request, plot_id: int, query_db: AsyncSession = Depends(get_db)):
    plot_detail_result = await PlotService.plot_detail_services(query_db, plot_id)
    logger.info(f'获取plot_id为{plot_id}的信息成功')

    return ResponseUtil.success(data=plot_detail_result)


@plotController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('plant:plot:export'))])
@Log(title='种植地块', business_type=BusinessType.EXPORT)
async def export_plant_plot_list(
    request: Request,
    plot_page_query: PlotPageQueryModel = Form(),
    query_db: AsyncSession = Depends(get_db),
):
    # 获取全量数据
    plot_query_result = await PlotService.get_plot_list_services(query_db, plot_page_query, is_page=False)
    plot_export_result = await PlotService.export_plot_list_services(request, plot_query_result)
    logger.info('导出成功')

    return ResponseUtil.streaming(data=bytes2file_response(plot_export_result))
